package com.xhdx.java8;

import static java.util.stream.Collectors.toList;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
/*
 * java8新特性之Stream
 */
public class Java8StreamTest {
    
    public static void main(String[] args) {
        
        List<People> list =new ArrayList<People>();
        
        list.add(new People("kang ming","1111111",68));
        list.add(new People("li ming","2222222",21));
        list.add(new People("wang ming","3333333",31));
        People p = new People("cui ming","4444444",20);
        list.add(p);
        list.add(p);
        
        System.out.println("排序显示前3个姓名:");
        List<String> list1 = list.stream().map(People::getName).sorted().limit(3).collect(Collectors.toList());
        list1.forEach(System.out::println);
        System.out.println("--------------------------");
        long count = list1.parallelStream().filter(string -> string.length()>=9).count();
        System.out.println("获取长度大于等于9的字符串数量:"+count);
        System.out.println("--------------------------");
        System.out.print("保留年龄>= 30 的 people 元素:");
        List<People> list2 = list.stream()
                    .filter(people -> people.getAge() >= 30)
                    .collect(toList());
        list2.forEach(pe -> System.out.print(" name="+pe.getName()+",age="+pe.getAge()));
        
        System.out.println();
        System.out.println("--------------------------");
        System.out.print("去掉前面一个元素");
        list2.stream().skip(1).collect(toList()).forEach(pe -> System.out.print(" name="+pe.getName()+",age="+pe.getAge()));
        
        System.out.println();
        System.out.println("--------------------------");
        System.out.println("未去掉重复元素:");
        List<People> list3 = list.stream()
                    .distinct()
                    .collect(toList());
        list.forEach(pe -> System.out.println(" name="+pe.getName()+",age="+pe.getAge()));
        System.out.println("去掉重复元素:");
        list3.forEach(pe -> System.out.println(" name="+pe.getName()+",age="+pe.getAge()));
        System.out.println("--------------------------");
        System.out.println("对流进行排序,对输出的 6个随机数进行排序:");
        Random random = new Random();
        random.ints().limit(6).sorted().forEach(System.out::println);
        
        System.out.println("--------------------------");
        System.out.println("flatMap:");
        List<String> list4 = new ArrayList<>();
        list4.add("aaa bbb ccc");
        list4.add("ddd eee fff");
        list4.add("ggg hhh iii");
        list4.stream().map(s -> s.split(" ")).flatMap(Arrays::stream).collect(toList()).forEach(System.out::println);
        
        System.out.println("--------------------------");
        System.out.println(" 是否存在一个 person 对象的 age 等于 20：");
        boolean b = list.stream().anyMatch(person -> person.getAge() == 27);
        System.out.println(b);
        System.out.println("--------------------------");
        List<Integer> numbers = Arrays.asList(305, 200, 22, 43, 87, 31, 15);
        IntSummaryStatistics stats = numbers.stream().mapToInt((x) -> x).summaryStatistics();
         
        System.out.println("列表中最大的数 : " + stats.getMax());
        System.out.println("列表中最小的数 : " + stats.getMin());
        System.out.println("所有数之和 : " + stats.getSum());
        System.out.println("平均数 : " + stats.getAverage());
    }
}
